package top.ahwyy.controller.Action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import top.ahwyy.controller.impl.cardImpl;
import top.ahwyy.controller.inter.card;
import top.ahwyy.model.userReplydata;
import top.ahwyy.utils.filterSpecialChar;

public class replysListAction implements ServletRequestAware, ServletResponseAware {

	public HttpServletResponse response;
	public HttpServletRequest request;
	public String forumId;
	
	public String execute(){
		response.setContentType("text/html;charset=utf-8");
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(forumId == null || forumId.equals(""))
			return "false";
		int fid = new Integer(forumId);
		card ci = new cardImpl();
		ArrayList<userReplydata> al = ci.getReplyList(fid);
		//String str = JSONArray.fromObject(al).toString();
		//System.out.print(str);
		String jsonStr = "[";
		for(userReplydata urd:al){
			jsonStr += "{\"content\":\""+filterSpecialChar.filterSpecialChar2(urd.getContent())+"\",\"floorNumber\":\""+urd.getFloorNumber()+"\",\"uid\":\""+urd.getUid()+"\",\"un\":\""+urd.getUn()+"\"},";
			
		}
		jsonStr = jsonStr.substring(0,jsonStr.length()-1);
		jsonStr+="]";
		try {
			PrintWriter pw = response.getWriter();
			pw.print(jsonStr);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "false";
		}
		
		
		return null;
	}
	
	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		this.response = arg0;

	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.request = arg0;
	}

}
